/**
 * ConnectionPostgres.java
 * Copyright (C) 2009 Jose Mauro da Silva Sandy
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package plagio.postgres;

import java.sql.Connection;
import java.sql.DriverManager;
import org.postgresql.Driver;

/**
 * This class have the objetive establish connection with databases postgresql.
 * @author Jose Mauro da Silva Sandy
 * @version Beta
 */
public class ConnectionPostgres implements IConnection{
    // <editor-fold defaultstate="collapsed" desc="Atributes">
    private final String DATABASE = "org.postgresql.Driver";
    private String user = "";
    private String passwd = "";
    private String url = "";
    private Connection connection = null;
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Accessors Methods.">
    /**
     * @return the connection
     */
    public Connection getConnection() {
        return connection;
    }
    /**
     * @param connection the connection to set
     */
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }
    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
    }
    /**
     * @return the passwd
     */
    public String getPasswd() {
        return passwd;
    }
    /**
     * @param passwd the passwd to set
     */
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }    
    // </editor-fold>

    /**
     *  @see Constructor default.
     */
    public ConnectionPostgres(){
    }
    /**
     *  @param user: receives the user of database.
     *  @param passwd: receives the password of database.
     *  @param url: receives one string in form: "server:port/database".
     *  @return Status to connection.
     */
    public boolean connection(String user, String passwd, String url){
        boolean connValid = true; // Valid connection.
        try{
           // Checking if all fields were passed.
            if(!user.isEmpty() && !passwd.isEmpty() && !url.isEmpty()){
                this.user = user;
                this.passwd = passwd;
                this.url = url;

                // Making the connection to the database.
                Class.forName(DATABASE);
                this.connection = (Connection) DriverManager.getConnection(
                        "jdbc:postgresql://" + url, user, passwd);
            }
            else{
                throw new Exception("Invalid arguments.");
            }
        }catch(Exception e){
            connValid = false;
            System.out.print(e.getMessage());
            e.printStackTrace();
        }
        return connValid;
    }
    /**
     * @see Close to connection.
     * @return Status to closing.
     */
    public boolean closeConnection(){
        boolean closeOk = true;
        try{
            if(this.connection != null){
                this.connection.close();
            }else{
                throw new Exception("Inexistent Connection.");
            }
        }catch(Exception e){
            closeOk = false;
            e.printStackTrace();
        }
        return closeOk;
    }
}
